package gaobo.queue;

import java.util.LinkedList;
import java.util.Queue;

public class MyDeStack {
    //面试题 1.用队列实现栈【力扣225】
    //使用两个队列实现一个栈
    //思路
    //入栈:入到不为空的队列当中
    //出栈:就是找到不为空的队列，出size-1个元素到另一个队列当中去
    private Queue<Integer> qu1;
    private Queue<Integer> qu2;

    public MyDeStack(){
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }

    public void push(int x){
        if (qu1.isEmpty()){
            qu1.offer(x);
        }else if (qu2.isEmpty()){
            qu2.offer(x);
        }else {
            //两个队列都为空的时候
            qu1.offer(x);
        }
    }

    public int pop(){
        if (empty()){
            return -1;
        }
        if (!qu1.isEmpty()){
            int size = qu1.size();
            for (int i = 0; i < size-1; i++) {
                qu2.offer(qu1.poll());
            }
            return qu1.poll();
        }else {
            int size = qu2.size();
            for (int i = 0; i < size-1; i++) {
                qu1.offer(qu2.poll());
            }
            return qu2.poll();
        }
    }
    //peek 获取栈顶元素
    public int top(){
        if (empty()){
            return -1;
        }
        if (!qu1.isEmpty()){
            int size = qu1.size();
            int ret = -1;
            for (int i = 0; i < size; i++) {
                ret = qu1.poll();
                qu2.offer(ret);
            }
            return ret;
        }else {
            int size = qu2.size();
            int ret = -1;
            for (int i = 0; i < size; i++) {
                ret = qu2.poll();
                qu1.offer(ret);
            }
            return ret;
        }
    }
    //两个队列都为空，那么就是栈空
    public boolean empty(){
        return qu1.isEmpty() && qu2.isEmpty();
    }

}
